#include <iostream>
#include <vector>

// 中介者接口定义与各组件通信的方法
class Mediator {
public:
    virtual void communicate(const std::string& message, class Colleague* colleague) = 0;
};

// 同事类接口，组件知晓中介者但不知晓其他同事类
class Colleague {
protected:
    Mediator* mediator;
public:
    Colleague(Mediator* m) : mediator(m) {}
    virtual void receiveMessage(const std::string& message) = 0;
    virtual void sendMessage(const std::string& message) = 0;
};

// 具体中介者，实现中介者接口，协调各组件间的交互
class ConcreteMediator : public Mediator {
private:
    std::vector<Colleague*> colleagues;
public:
    void addColleague(Colleague* colleague) {
        colleagues.push_back(colleague);
    }

    void communicate(const std::string& message, Colleague* originator) override {
        for (auto* colleague : colleagues) {
            if (colleague != originator) {  // 发送信息给除了发起者之外的所有同事
                colleague->receiveMessage(message);
            }
        }
    }
};

// 具体同事类A
class ConcreteColleagueA : public Colleague {
public:
    ConcreteColleagueA(Mediator* m) : Colleague(m) {}

    void receiveMessage(const std::string& message) override {
        std::cout << "同事A收到消息: " << message << std::endl;
    }

    void sendMessage(const std::string& message) override {
        std::cout << "同事A发送消息: " << message << std::endl;
        mediator->communicate(message, this);
    }
};

// 具体同事类B
class ConcreteColleagueB : public Colleague {
public:
    ConcreteColleagueB(Mediator* m) : Colleague(m) {}

    void receiveMessage(const std::string& message) override {
        std::cout << "同事B收到消息: " << message << std::endl;
    }

    void sendMessage(const std::string& message) override {
        std::cout << "同事B发送消息: " << message << std::endl;
        mediator->communicate(message, this);
    }
};

int main() {
    ConcreteMediator mediator;
    ConcreteColleagueA colleagueA(&mediator);
    ConcreteColleagueB colleagueB(&mediator);

    mediator.addColleague(&colleagueA);
    mediator.addColleague(&colleagueB);

    colleagueA.sendMessage("你好，同事B");
    colleagueB.sendMessage("你好，同事A，今天感觉怎么样？");

    return 0;
}